﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Realtors.Entity;

namespace Realtors.Repository.Fake
{
    public class FakeHouseRepository:IHouseRepository
    {
        public static IQueryable<House>Houses { get; set; }
        private static IQueryable<House> GenerateFakeHouses()
        {
            PropertyType apartment = new PropertyType() {Name = "Apartment", EntityId = new Guid()};
            List<House>allHouses = new List<House>();
            for (int i = 1; i <= 150; i++)
            {
                House house = new House();
                house.Active = i%7 != 0;
                house.DateAdded = DateTime.UtcNow.AddDays((-i*7));
                house.DateLeased = DateTime.MinValue;
                house.DateSold = DateTime.MinValue;
                house.Description = string.Format("Description {0} {1}", i, new string('d', i * 50));
                house.EntityId = new Guid();
                house.Featured = i%9 == 0;
                house.ForLease = i%10 == 0;
                house.ForSale = !house.ForLease;
                house.Title = string.Format("Title {0} {1}", i, new string('t', i + 10));
                house.HouseAddress.City = new string('c',i+20);
                house.HouseAddress.Country = "USA";
                house.HouseType = apartment;
                house.HouseAddress.State = "CA";
                house.HouseAddress.StreetAddress = new string('s',i+40);
                house.LeasingPrice = house.ForLease == true ? 2000 : decimal.MinValue;
                house.NoOfBathrooms = (i%4)+1;
                house.NoOfRooms = (i%7) + 1;
                house.PropertyPhotos = new List<string>{("~/Content/img/slide-1.jpg")};
                house.SellingPrice = house.ForSale == true ? 50000*i : decimal.MinValue;
                house.SquareFeet = 1000 + i*2;
                house.EntityId = Guid.NewGuid();
                house.TotalVisits = i * 2;
                house.Deleted = i % 11 == 0;
                if (house.Deleted == true)
                    house.DateDeleted = DateTime.UtcNow.AddDays((-i * 5));
                allHouses.Add(house);
                
            }
            return allHouses.AsQueryable();
        }

         static FakeHouseRepository()
        {
            Houses = GenerateFakeHouses();
        }

        public void Dispose()
        {
            return;
        }

        public IQueryable<House> GetActiveHouses()
        {
            return Houses.Where(c=>c.Deleted==false).Where(c=>c.Active==true);
        }

        public IQueryable<House> GetActiveFeaturedHouses()
        {
            return Houses.Where(c => c.Deleted == false).Where(c => c.Active == true).Where(c => c.Featured == true);
        }
        public House GetHouseByID(Guid houseID)
        {
            return Houses.Single(c => c.EntityId == houseID);
        }

        public void InsertHouse(House house)
        {
            var houseList = Houses.ToList();
            houseList.Add(house);
            Houses = houseList.AsQueryable();
        }

        public void DeleteHouse(Guid houseID)
        {
            var houseList = Houses.ToList();
            House house = houseList.Single(c => c.EntityId == houseID);
            houseList.Remove(house);
            Houses = houseList.AsQueryable();
        }

        public void UpdateHouse(House house2Update)
        {
            var houseList = Houses.ToList();
            House house = houseList.Single(c => c.EntityId == house2Update.EntityId);
            DeleteHouse(house2Update.EntityId);
            InsertHouse(house2Update);
            Houses = houseList.AsQueryable();
        }

        public void Save()
        {
            return;
        }
    }
}
